/*
 * > /*
 >  * INSANE - Interactive Structural Analysis Environment
 >  *
 >  * Copyright (C) 2003-2004
 >  * Universidade Federal de Minas Gerais
 >  * Escola de Engenharia
 >  * Departamento de Engenharia de Estruturas
 >  *
 >  * Author's email :     insane@dees.ufmg.br
 >  * Author's Website :   http://www.dees.ufmg.br/insane
 >  *
 >  * This program is free software; you can redistribute it and/or
 >  * modify it under the terms of the GNU General Public License
 >  * as published by the Free Software Foundation; either version 2
 >  * of the License, or any later version.
 >  *
 >  * This program is distributed in the hope that it will be useful,
 >  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 >  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 >  * GNU General Public License for more details.
 >  *
 >  * You should have received a copy of the GNU General Public License
 >  * along with this program; if not, write to the Free Software
 >  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 > 1307, USA.
 *
 */
package br.ufmg.dees.insane.ui.rich.learn.learn_controller;


import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ListIterator;

import br.ufmg.dees.insane.draw.Draw;
import br.ufmg.dees.insane.draw.GaussPointDraw;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.element.ParametricElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.integrationpoint.IntegrationPoint;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.IView;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.element_view.ElementView;
import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.IVector;


/**
 * 
 * @author Moreira, Renata & Pitangueira, Roque
 * @since August 2005
 */
public class StrainController extends LearnController {
	
   
    
	public StrainController(FemModel model) {
        this.model = model;
    }

    
    public StrainController(FemModel model, IView eview) {
        this.model = model;
        this.view = eview;
    }

    
    public StrainController(IView eview) {
        this.view = eview;
    }

    public StrainController() {
    }
    
    public void setModel(FemModel newModel) {
        this.model = newModel;
    }

    public void setView(ElementView newView) {
        this.view = newView;
    }
    
    public void draw(Graphics2D g) {
        super.draw(g);
        //Draws the gauss Marks
		Draw draw;
		IntegrationPoint ip;
		double nodeScale = view.getViewState().nodeScale;
		g.setColor(view.getViewState().pointColor);
		ListIterator pts = this.gaussMarks.listIterator();
		while(pts.hasNext()) {
			draw = (Draw)pts.next();
			ip = (IntegrationPoint)draw.getRepresents();
			draw.setScale(nodeScale);
			draw.setLabel(ip.getLabel());
			draw.draw(g);
			
		}
    }
    
    public void select(Point p) {
		Draw draw;
		int index;
		ListIterator pts = gaussMarks.listIterator();
        while (pts.hasNext()) {
            draw = (Draw) pts.next();
            if (draw.contains(p)) {
                if (draw.isSelected()) {
                    draw.setSelected(false);
                    //index = selection.indexOf(draw.getRepresents());                   
                    index = selection.indexOf(draw);
                    selection.remove(index);
                    return;
                } else {
                    if (draw.isSelectable()) {
                       // this.unSelectAll();
                        draw.setSelected(true);
                        //selection.add(draw.getRepresents());
                        selection.add(draw);
                        }
                    return;
                }
            }
        }
        
        
	}
    
    public void selectAll() {
        Draw draw;
		selection.clear();
		ListIterator pts = gaussMarks.listIterator();
        while (pts.hasNext()) {
            draw = (Draw) pts.next();
            draw.setSelected(true);
            //selection.add(draw.getRepresents());
            selection.add(draw);
        }
        
        
	}
    
    public void unSelectAll() {
        Draw draw;
		selection.clear();
		ListIterator pts = gaussMarks.listIterator();
        while (pts.hasNext()) {
            draw = (Draw) pts.next();
            draw.setSelected(false);
        }
	}
    
    public boolean isSelected() {
        Draw draw;
        ListIterator pts = gaussMarks.listIterator();
        while (pts.hasNext()) {
            draw = (Draw) pts.next();
            if (draw.isSelected()) {
                return true;
            }
        }
        return false;
  }
    
    public void compose() {
        this.removeAll();
        super.compose();
        //Compose the Gauss Point marks for parametric elements
		Point2D po;
		Point p;
        ListIterator elms = model.getElementsList().listIterator();
        Element elm;
		while (elms.hasNext()) {
			elm = (Element)elms.next();
			if (elm instanceof ParametricElement){
			    ParametricElement pelm = (ParametricElement)elm;
				ListIterator ElmGaussPts = pelm.getIntegrationPointsList().listIterator();
				while (ElmGaussPts.hasNext()){
					IntegrationPoint ip = (IntegrationPoint)ElmGaussPts.next();
					IMatrix N = pelm.mountN(ip);
					IVector coords = pelm.mountCoords();
					IVector coord = N.mul(coords);
				    po = new Point2D.Double(coord.getElement(0),coord.getElement(1));
				    p = view.getDrawingArea().getViewPoint(po);
				    gaussMarks.add(new GaussPointDraw(p, ip, pelm));
				}
			}
		}
	}	
        
    
        
        
}